package com.shelpe.services.scheduler.item.creative;

import org.springframework.batch.item.ItemProcessor;
import org.springframework.beans.factory.annotation.Autowired;

import com.shelpe.services.scheduler.dto.CreativesDto;
import com.shelpe.services.scheduler.mongorepository.UnrecoverableErrorRepository;
import com.shelpe.services.scheduler.po.Adgroups;
import com.shelpe.services.scheduler.po.SyncLog;
import com.shelpe.services.scheduler.syncclient.CreativeClient;
import com.shelpe.services.scheduler.util.Util;

public class CreativeProcessor implements ItemProcessor<Adgroups, SyncLog>{

	@Autowired
	private CreativeClient creClient;

	@Autowired
	private UnrecoverableErrorRepository errorRep;
	
	@Override
	public SyncLog process(Adgroups item) throws Exception {
		// TODO Auto-generated method stub

		long userId = item.getUserId();
		if(errorRep.countByUserId(userId) > 0){
			return null;
		}
		
		long adgroupId = item.getAdgroupId();
		CreativesDto dto = creClient.syncCreativesByUserId(userId, adgroupId);
		
		return Util.convert(dto);
	}

}
